import groovy.beans.Bindable

class LocalFileViewerModel {
	//set by TermController
	def mvcId
	def parentController
	
   @Bindable String currentFolder=System.getProperty('user.home')
   @Bindable def folderContents=[]
   @Bindable boolean anySelected

    void refresh()
    {
        folderContents=[]
        def filesWithin=[]

        folderContents<< [type:'D',name:'..',size:'-']
        new File(currentFolder).eachFile{
            def item = [type: it.directory ? 'D' : 'F', name: it.name, size:it.size(),modified: new Date(it.lastModified()) ]
            if(it.isDirectory()) {
              folderContents<< item
            } else {
              filesWithin<< item
            }
        }
        //folders first, files at the end
        def comparator = [compare: { o1,o2 -> return o1.name.compareTo(o2.name) }] as Comparator;
        Collections.sort(folderContents, comparator)
        Collections.sort(filesWithin, comparator)
        folderContents.addAll(filesWithin) 
    }

}